using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;

namespace nzmq.tests
{
    #region Experimental
    [TestFixture]
    public class use_Exception_handling
    {
        [Test, Ignore]
        public void Handling_exceptions()
        {
            using(var ctx = new NzmqContext())
            using(var csck = ctx.CreateClientSocket("inproc://service"))
            {
                var are = new AutoResetEvent(false);
                Exception exCaught=null;

                using (ctx.Catch("inproc://exceptions", ex => { exCaught = ex; are.Set(); }))
                {
                    csck.Send("hello");

                    Task.Factory.StartNew(() =>
                                              {
                                                  using(var ssck = ctx.CreateServiceSocket("inproc://service"))
                                                  {
                                                      ctx.Try(() =>
                                                        {
                                                            Console.WriteLine("received: {0}", ssck.Receive());
                                                            throw new InvalidOperationException("argh!");
                                                        });
                                                  }
                                              });
                }

                Assert.IsTrue(are.WaitOne(500));
                Assert.IsInstanceOf<InvalidOperationException>(exCaught);
            }
        }
    }
    #endregion
}